<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div>
        <img
          src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3351025671,3021710149&fm=26&gp=0.jpg"
          alt=""
        />
        <span>柴犬售后部</span>
      </div>
      <el-button type="danger" @click="logout">退出</el-button></el-header
    >
    <!-- 左部栏 -->
    <el-container>
      <el-aside :width="iscollapse ? '60px' : '200px'">
        <div class="togger-btn" @click="changeCollapse">|||</div>
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="lightgoldenrodyellow"
          text-color="lightcoral"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu
            v-for="item in menuList"
            :key="item.id"
            :index="item.id + ''"
          >
            <template slot="title">
              <i :class="menuIcon[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <!-- 坑：路径一定要加 / ，不然后面二级路径跳不回一级路径 -->
            <el-menu-item
              :index="'/' + value.path"
              v-for="value in item.children"
              :key="value.id"
              @click="saveActive('/' + value.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ value.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      //一级菜单图标
      menuIcon: {
        125: "el-icon-s-custom",
        103: "el-icon-s-platform",
        101: "el-icon-s-goods",
        102: "el-icon-s-shop",
        145: "el-icon-s-data",
      },
      iscollapse: false,
      activePath: ''
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("active")
  },
  methods: {
    //登出功能
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取菜单数据
    async getMenuList() {
      let { data: res } = await this.$http.get("menus");
      console.log(res);
      this.menuList = res.data;
    },
    //折叠功能
    changeCollapse() {
      this.iscollapse = !this.iscollapse;
    },
    //获取当前索引存在sessionStorage中，激活当前高亮颜色
    saveActive(activePath){
       window.sessionStorage.setItem("active",activePath)
       this.activePath = activePath
    }
  },
};
</script>

<style lang="less" scoped> 
.el-container {
  height: 100%;
  .el-header {
    background-color: lemonchiffon;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      display: flex;
      align-items: center;
      color: lightgrey;
      img {
        width: 60px;
        height: 60px;
        border-radius: 50%;
      }
      span {
        font-size: 20px;
        margin-left: 10px;
      }
    }
  }

  .el-aside {
    background-color: lightgoldenrodyellow;

    .togger-btn {
      color: lightcoral;
      cursor: pointer;
      letter-spacing: 0.1em;
    }

    .el-menu {
      border: none;

      .el-menu-item {
        font-size: 12px;
      }
    }
  }

  .el-main {
    background-color: lightyellow;
  }
}
</style>